package com.youpingou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youpingou.domain.News;
import com.youpingou.mapper.NewsMapper;
import com.youpingou.service.NewsService;
import com.youpingou.vo.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Override
    public Page findNewsByPage(PageRequest pageRequest) {
        //1 条件
        QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
        if("asc".equals(pageRequest.getSortWay())) {
            queryWrapper.orderByAsc("created_at");
        } else {
            queryWrapper.orderByDesc("created_at");
        }

        //2 分页
        Page<News> page = new Page<>(pageRequest.getPage(), pageRequest.getPagesize());

        //3 查询
        baseMapper.selectPage(page, queryWrapper);

        return page;
    }
}
